package ar.com.niktutos.dao.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;

import ar.com.niktutos.dao.CapituloDAO;
import ar.com.niktutos.dto.CapituloDTO;
import ar.com.niktutos.dto.TagDTO;

public class CapituloDAOImpl extends AbstractGenericDAOImpl<CapituloDTO,Integer> implements CapituloDAO{
	   public List<CapituloDTO> findAll() {
		   	 Session sess = sessionFactory.openSession();
			 Transaction tx = null;
			 List<CapituloDTO> ret = new ArrayList<CapituloDTO>();
			 try {
			     tx = sess.beginTransaction();
			     Query query = sess.createQuery("from CapituloDTO");
		         List<CapituloDTO> lista = query.list();
		         if(lista!=null)ret = lista;
			     tx.commit();
			 }
			 catch (Exception e) {
			     if (tx!=null) tx.rollback();
			     e.printStackTrace();
			 }
			 finally {
			     sess.close();
			 }
			 return ret;
				 
	    }

	public CapituloDTO getLastCapitulo() {
	   	 Session sess = sessionFactory.openSession();
		 Transaction tx = null;
		 CapituloDTO retorna = null;
		 try {
		     tx = sess.beginTransaction();
	         Query query = sess.createQuery("from CapituloDTO c order by c.numeroCapitulo desc");
		     query.setMaxResults(1);
		     query.setFirstResult(1);
		     List<CapituloDTO> lista = query.list();
		     if(lista != null && lista.size() > 0)
		    	 retorna = (CapituloDTO) lista.get(0);
		     tx.commit();
		 }
		 catch (Exception e) {
		     if (tx!=null) tx.rollback();
		     e.printStackTrace();
		 }
		 finally {
		     sess.close();
		 }
		 return retorna;
	}

	@Override
	public CapituloDTO getCapituloByNumber(Integer capNumber) {
	   	 Session sess = sessionFactory.openSession();
		 Transaction tx = null;
		 CapituloDTO retorna = null;
		 try {
		     tx = sess.beginTransaction();
	         Query query = sess.createQuery("from CapituloDTO c where c.numeroCapitulo = ?");
		     query.setInteger(0, capNumber);
		     List<CapituloDTO> lista = query.list();
		     if(lista != null && lista.size() > 0)
		    	 retorna = (CapituloDTO) lista.get(0);
		     tx.commit();
		 }
		 catch (Exception e) {
		     if (tx!=null) tx.rollback();
		     e.printStackTrace();
		 }
		 finally {
		     sess.close();
		 }
		 return retorna;
	}

	@Override
	public Collection<CapituloDTO> findCapitulos(String s) {
	   	 Session sess = sessionFactory.openSession();
		 Transaction tx = null;
		 List<CapituloDTO> ret = new ArrayList<CapituloDTO>();
		 try {
		     tx = sess.beginTransaction();
		    
	         Query query = sess.createQuery("select distinct c from CapituloDTO as c join c.tags as t where c.numeroCapitulo like '%"+s+"%' or c.descripcion like '%"+s+"%' or c.nombre like '%"+s+"%' or t.descripcion like '%"+s+"%'");
//		     query.setString(0, s);
//		     query.setString(1, s);
//		     query.setString(2, s);
	         List<CapituloDTO> lista = query.list();
	         if(lista!=null)ret = lista;
		     tx.commit();
		 }
		 catch (Exception e) {
		     if (tx!=null) tx.rollback();
		     e.printStackTrace();
		 }
		 finally {
		     sess.close();
		 } 
		 return ret;
	}

}
